﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Mvc;
using System.ComponentModel;
using EIIS.Infrastructure.Service.System.Authority;
using EIIS.Infrastructure.Model.System.Authority;
using EIIS.Presentation.Controllers.CommonSupport;
using EIIS.Presentation.Controllers.ViewModel;
using EIIS.Common.Jsons;
using EIIS.FrameWork.Attributes;

namespace EIIS.Presentation.Controllers.System.Authority
{   
    [Description("功能权限管理")]
    public class ActionPermissionController : BaseController
    {
        public IActionPermissionManager ActionPermissionManager { get; set; }

        [Description("功能权限管理页面")]
        [ViewPage]
        public ActionResult Index()
        {           
            return View();
        }

        public ActionResult LoadAllWithPage(int page, int rows, string order, string sort)
        {
            long total = 0;
            IList<ActionPermission> ilist = ActionPermissionManager.LoadAllWithPage(out total, page, rows,order,sort).OrderBy(a => a.ControllerName).ThenBy(a => a.ActionName).ToList();
            return this.JsonFormat(new GridData { total = total, rows = ilist.ToList() });
        }

        public ActionResult LoadAll()
        {
            long total = 0;
            IList<ActionPermission> ilist = ActionPermissionManager.LoadAll().OrderBy(a => a.ControllerName).ThenBy(a => a.CreateTime).ToList();
            return this.JsonFormat(new GridData { total = total, rows = ilist.ToList() });
        }

        public ActionResult LoadAllByAssembly(int page, int rows)
        {
            string strKeyword = Request["q"];
            long total = 0;
            IList<ActionPermission> ilist = ActionPermissionManager.GetAllActionByAssembly(out total, page, rows, "", "", strKeyword);
            return this.JsonFormat(new GridData { total = total, rows = ilist.ToList() });
        }

        [Description("新增功能权限")]
        [Function(ButtonID = "btnAdd")]
        public ActionResult Add()
        {
            ActionPermission entity = new ActionPermission();
            this.ViewData["entity"] = entity;
            return View();
        }

        [Description("修改功能权限")]
        [Function(ButtonID = "btnUpdate")]
        public ActionResult Edit(string id)
        {
            ActionPermission entity = this.ActionPermissionManager.Get(id);
            this.ViewData["entity"] = entity;
            return View();
        }

        [Description("删除功能权限")]
        [Function(ButtonID = "btnDelete")]
        public ActionResult Delete(IList<string> idList)
        {
            MessageInfo msg = new MessageInfo();
            msg.IsSuccess = true;
            msg.Message = "删除成功";
            ActionPermissionManager.Delete(idList.Cast<object>().ToList());
            return Json(msg);
        }

        [Description("保存功能权限记录")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(ActionPermission entity)
        {
            MessageInfo msInfo = new MessageInfo();
            IList<ActionPermission> list;
            string strHql = "";
            
            if (string.IsNullOrEmpty(entity.Id))//新增
            {
                strHql = "from ActionPermission a where a.ActionName='"+entity.ActionName+"' and a.ControllerName='"+entity.ControllerName+"'";
                list = ActionPermissionManager.Find(strHql);
                if (list.Count() > 0)
                {
                    msInfo.IsSuccess = false;
                    msInfo.Message = "新增失败，已存在该功能权限的记录";
                    return Json(msInfo);
                }
                entity.Id = Guid.NewGuid().ToString();

                this.ActionPermissionManager.Save(entity);
            }
            else//修改
            {
                var model = this.ActionPermissionManager.Get(entity.Id);
                model.Description = entity.Description;
                this.ActionPermissionManager.Update(model);
            }            
            msInfo.IsSuccess = true;
            msInfo.Message = "保存成功";
            return Json(msInfo, "text/html", JsonRequestBehavior.AllowGet);
        }
    }
}
